# -*- coding: utf-8 -*-
"""
使用自己的SGD
Created on Thu Feb 22 12:06:32 2018

@author: Allen
"""

import numpy as np
import matplotlib.pyplot as plt

m = 100000
x = np.random.normal( size = m )
X = x.reshape( -1, 1 )
y = 4.0 * x + 3.0 + np.random.normal( 0, 3, size = m )

from playML.LinearRegression import LinearRegression
'''
lin_reg = LinearRegression()
lin_reg.fit_sgd( X, y, n_iters = 2 )
print( lin_reg.coef_,lin_reg.intercept_ ) # 输出 3.99649879787 2.99248449303
'''
# 使用真实数据测试随机梯度下降
#
from sklearn import datasets

boston = datasets.load_boston()
X = boston["data"]
y = boston["target"]

X = X[y<50]
y = y[y<50]

from playML.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split( X, y, seed = 666 )

from sklearn.preprocessing import StandardScaler
ss = StandardScaler()
ss.fit( X_train )
X_train_standard = ss.transform( X_train )
X_test_standard = ss.transform( X_test )

lin_reg1 = LinearRegression()
lin_reg1.fit_sgd( X_train_standard, y_train, n_iters = 50 )
print( lin_reg1.coef_, lin_reg1.intercept_ )
print( lin_reg1.score( X_test_standard, y_test ) )
'''
n_iters = 2  0.792332955543
n_iters = 10 0.812991826118
n_iters = 50 0.81308202702
由此可见，循环的轮数还是非常重要的
'''

'''
sklearn中的sgd
'''
from  sklearn.linear_model import SGDRegressor
sgd_reg = SGDRegressor( n_iter = 50 )
sgd_reg.fit( X_train_standard, y_train )
print( sgd_reg.score( X_test_standard, y_test ) )
'''
n_iter = 5(默认) 0.805397256495
n_iter = 50 0.813348078418
'''

'''
注意：
    随机梯度下降封装在了sklearn.linear_model包，说明其只能解决线性模型
'''










